#include "occipool.hpp"
#include <iostream>


using namespace com_oocl_riley;


OcciPool::OcciPool(const string & constr, const string & usr, const string & pwd, int maxCon, int minCon, int incrCon)
: _constr(constr), _usr(usr), _pwd(pwd), _maxCon(maxCon), _minCon(minCon), _incrCon(incrCon) {
    _occiEnv = OcciEnv::getInstance();
    _pool = _occiEnv->Env()->createStatelessConnectionPool(usr, pwd, constr, maxCon, minCon, incrCon, StatelessConnectionPool::HOMOGENEOUS);
    sem_init(&_sem,0,maxCon);
}

OcciPool::~OcciPool() {
    _occiEnv->Env()->terminateStatelessConnectionPool(_pool);
}

Connection *OcciPool::getConnection() {
    Connection *rs = nullptr;
    sem_wait(&_sem);
    rs=_pool->getConnection();
    return rs;
}

void OcciPool::releaseConnection(Connection *con) {

    _pool->releaseConnection(con);
    sem_post(&_sem);
}
